<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/2/26
 * Time: 20:19
 */

namespace App\Http\Controllers\Api\Open;


use App\Http\Resources\ChannelCollection;
use App\Http\Resources\LectureCollection;
use App\Http\Resources\Liveroom as LiveroomResources;
use App\Http\Resources\LiveroomBannerCollection;
use App\Http\Resources\UserCollection;
use App\Models\Channel;
use App\Models\Lecture;
use App\Models\Liveroom;
use App\Models\LiveroomBanner;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class LiveroomController extends BaseController
{
    public function index()
    {
        $user = $this->getUser();
        $liveroom = Liveroom::query()->firstOrCreate(['user_id' => $user->id], [
            'user_id' => $user->id,
            'title' => $user->first_name,
            'description' => $user->first_name,
            'avatar' => $user->wx_user->avatar,
            'qrcode' => ''
        ]);
        $data = new LiveroomResources($liveroom);
        return $this->success($data);
    }

    public function update(Request $request)
    {
        $user = $this->getUser();
        $validator = \Validator::make($request->all(), [
            'title' => ['required', Rule::unique('liveroom')->ignore($user->id, 'user_id')],
            'description' => 'required|max:30',
            'avatar' => 'required'
        ]);
        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }
        $liveroom = Liveroom::query()->where('user_id', $user->id)->first();
        $liveroom->update([
            'title' => $request->input('title'),
            'description' => $request->input('description'),
            'avatar' => $request->input('avatar'),
            'qrcode' => $request->input('qrcode'),
        ]);

        return $this->success(new LiveroomResources($liveroom));

    }

    public function banner()
    {
        $user = $this->getUser();
        $banner = LiveroomBanner::query()->where('user_id', $user->id)->orderByDesc('priority')->get();

        return $this->success(LiveroomBannerCollection::make($banner));
    }

    public function del_banner(Request $request)
    {
        $id = $request->input("id", false);
        $id = hashid_decode($id);
        if ($id <= 0) {
            return $this->failed('id error');
        }
        $user = $this->getUser();
        $banner = LiveroomBanner::query()->where('user_id', $user->id)->findOrFail($id);
        if ($banner->delete()) {
            return $this->message('删除成功');
        } else {
            return $this->failed('删除失败');
        }

    }

    public function before_edit_banner(Request $request)
    {
        $user = $this->getUser();
        $data['channel'] = ChannelCollection::make(Channel::query()->where('user_id', $user->id)->get())->show(['id', 'name']);//专栏
        $data['lecture'] = LectureCollection::make(Lecture::query()->where('user_id', $user->id)->get())->type('open_list')->show(['id', 'name']);//课程
        $id = $request->input("id", false);
        if ($id) {
            $id = hashid_decode($id);
            if ($id <= 0) {
                return $this->failed('id error');
            }
            $banner = LiveroomBanner::query()->where('user_id', $user->id)->findOrFail($id);
            $data['banner'] = \App\Http\Resources\LiveroomBanner::make($banner)->hide(['link_name']);
        }

        return $this->success($data);

    }

    public function edit_banner(Request $request)
    {
        $user = $this->getUser();
        $validator = \Validator::make($request->all(), [
            'cover' => ['required'],
            'priority' => 'required',
            'link_type' => 'required',
            'link_id' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }
        $id = $request->input("id", false);
        if ($id) {
            $id = hashid_decode($id);
            if ($id <= 0) {
                return $this->failed('id error');
            }
        }
        $edit_data['cover'] = $request->input("cover");
        $edit_data['priority'] = $request->input("priority");
        $edit_data['link_type'] = $request->input("link_type");
        $edit_data['link_id'] = hashid_decode($request->input("link_id"));
        $edit_data['user_id'] = $user->id;
        $edit_data['liveroom_id'] = $user->liveroom->id;
        if ($id > 0) {
            $banner = LiveroomBanner::query()->where('user_id', $user->id)->findOrFail($id);
            $banner->update($edit_data);
        } else {
            $banner = LiveroomBanner::query()->create($edit_data);
        }

        return $this->message('保存成功');

    }

    public function fans(Request $request)
    {
        $s_name = $request->input("s_name", false);
        $user = $this->getUser();
        $liveroom = $user->liveroom;
        $orm = $liveroom->followers();
        if ($s_name) {
            $orm->leftJoin('wx_user', 'users.id', '=', 'wx_user.user_id')->where("nickname", 'like', '%' . $s_name . '%')->orWhere('name', 'like', '%' . $s_name . '%');
        }
        $page = $orm->paginate();
        $data = UserCollection::make($page)->type('fans');
        return $this->success($data);

    }

}